#pragma once
#include "feSingleton.h"
#include <list>
#include <iostream>
#include <stdarg.h>

class feObserver
{
public:
	virtual void Notify(const char *iMsg) = 0;
};


//---------------------------------------------------------------------------------------------------------------------------------------------
class feSimpleObserver : public feObserver
{
public:
	virtual void Notify(const char *iMsg)
	{
		std::cout << iMsg;
	}
};


//---------------------------------------------------------------------------------------------------------------------------------------------
class feObserverManager : public feSingleton<feObserverManager>
{
	std::list<feObserver *> Observers;

public:
	void Notify(char *iFormat, ...)
	{
		if(NULL == iFormat)
			return;

		char msg[1024];

		//create msg
		va_list vap;
		va_start(vap, iFormat);
		_vsnprintf_s(msg, 1024, 1024, iFormat, vap); // Sync. with MSDN
		va_end(vap);


		for(std::list<feObserver *>::iterator i = Observers.begin(); i != Observers.end(); ++i)
			(*i)->Notify(msg);
	}


	//------------------------------------------------------------------------------------------------------------------------------------------
	void Register(feObserver *iObs)
	{
		Observers.push_back(iObs);
	}


	//------------------------------------------------------------------------------------------------------------------------------------------
	void Unregister(feObserver *iObs)
	{
		//find and delete observer
		for(std::list<feObserver *>::iterator i = Observers.begin(); i != Observers.end(); ++i)
		{
			if(*i == iObs)
			{
				Observers.erase(i);
				return;
			}
		}
	}
};